package com.study.springmvc.handler;

import com.study.springmvc.controller.ThreadLocalContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Component
public class TestHandler {

    @Autowired
    @Qualifier("taskExecutor-2")
    private ThreadPoolExecutor taskExecutor;

    @Async("taskExecutor-1")
    public void testAsync() {
        //从ThreadLocal中获取traceId
        String traceId = ThreadLocalContext.getTraceId();
        System.out.println("traceId-->" + traceId);
        System.out.println("子线程名称-->" + Thread.currentThread().getName());

        CompletableFuture.runAsync(() -> {
            //从ThreadLocal中获取traceId
            String traceId2 = ThreadLocalContext.getTraceId();
            System.out.println("traceId2-->" + traceId2);
            System.out.println("子线程名称2-->" + Thread.currentThread().getName());
        }, taskExecutor);
    }
}
